import { glob } from "glob";
import { writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { format } from "prettier";

import { alphaNumericSort } from "../src/utils/alphaNumericSort.js";
import { PRIVATE_FILES } from "./constants.js";

const files = await glob("**/*.{ts,tsx}", {
  cwd: join(process.cwd(), "src"),
  ignore: ["**/__tests__/**", "**/test-utils/**", ...PRIVATE_FILES],
});

const sorted = alphaNumericSort(files);
await writeFile(
  resolve("../react-md/src/index.ts"),
  await format(
    `// THIS FILE WAS GENERATED BY A SCRIPT AND SHOULD NOT BE UPDATED MANUALLY

${sorted.map((name) => `export * from "@react-md/core/${name.replace(/\.tsx?$/, "")}"`).join(";\n")}
`,
    { parser: "typescript" }
  )
);
